package b4.net
{
	import b4.util.profile.*;
	
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	
	public class ResourceManager
	{
		static private var instance:ResourceManager;
		private var appDomains:Object;
		
		public function ResourceManager()
		{
			appDomains = new Object();
		}

		static public function getInstance():ResourceManager
		{
			if (!instance)
				instance = new ResourceManager();
			return instance;
		}
	    
	    public function loadResources(urlNames:Array):ResourceLoader
	    {
			//加载图形资源
			var resourceLoader:ResourceLoader = new ResourceLoader();
			resourceLoader.loadResources(urlNames);
			resourceLoader.addEventListener(Event.COMPLETE, onResourcesLoaded);
			return resourceLoader;
	    }
	    
	    private function onResourcesLoaded(event:Event):void
	    {
	    	var resourceLoader:ResourceLoader = event.currentTarget as ResourceLoader;
	    	for (var i:int = 0; i < resourceLoader.urlNames.length; i++)
	    	{
	    		var urlName:String = resourceLoader.urlNames[i]; 
	    		appDomains[urlName] = resourceLoader.appDomains[i];
	    	}
	    }
	    
	    public function getClassByName(name:String):Class
	    {
	    	for (var appStr:String in appDomains)
	    	{
	    		var appDomain:ApplicationDomain = appDomains[appStr];
	    		try
	    		{
	    			return appDomain.getDefinition(name) as Class;
	    		}
		    	catch (e:Error)
		    	{
		    		
		    	}	
	    	}
	    	return null;
	    }
	    
	}
}